#include<iostream>
#include<vector>
#include<cstring>

using namespace std;

vector<int> ans;
bool visit[9] = { false };

bool valid(int x, int y, vector<pair<int, int>> sequence)
{
    for (int i = 0; i < sequence.size(); i++)
    {
        if (abs(x - sequence[i].first) == abs(y - sequence[i].second))
        {
            return false;
        }
    }
    return true;
}


void DFS(int x, vector<pair<int, int>> sequence)
{
    if (sequence.size() == 8)
    {
        int n = 0;
        for (int i = 0; i < 8; i++)
        {
            n *= 10;
            n += sequence[i].second;
        }
        ans.push_back(n);
        return;
    }
    for (int y = 1; y <= 8; y++)
    {
        if (visit[y] || !valid(x, y, sequence))
        {
            continue;
        }
        visit[y] = true;
        sequence.push_back(pair<int, int>(x, y));
        DFS(x + 1, sequence);
        sequence.pop_back();
        visit[y] = false;
    }
}





int main()
{
    vector<pair<int, int>> sequence;
    DFS(1, sequence);
    int n;
    while (cin >> n)
    {
        cout << ans[n - 1] << endl;
    }
}